import json
import random
import itertools

def generate_test_cases():
    """生成全排列算法的测试用例"""
    test_cases = []
    
    # 基础测试用例
    basic_cases = [
        # 简单情况
        ([1], "单个元素"),
        ([1, 2], "两个元素"),
        ([1, 2, 3], "三个元素"),
        ([1, 2, 3, 4], "四个元素"),
        
        # 包含重复数字（虽然题目说不重复，但测试边界）
        ([0, 1], "包含0"),
        ([1, 0], "0和1"),
        ([2, 1, 0], "倒序"),
        
        # 负数情况
        ([-1, 0, 1], "包含负数"),
        ([-2, -1], "全负数"),
        
        # 较大数字
        ([5, 4, 3], "较大数字"),
        ([10, 20, 30], "更大数字"),
    ]
    
    for case, desc in basic_cases:
        # 计算期望结果
        expected = list(itertools.permutations(case))
        expected = [list(perm) for perm in expected]
        
        test_cases.append({
            "input": case,
            "expected": expected,
            "description": desc,
            "expected_count": len(expected)
        })
    
    # 边界情况
    edge_cases = [
        # 空数组
        ([], "空数组"),
        
        # 单元素不同值
        ([0], "单个0"),
        ([5], "单个5"),
        ([-1], "单个负数"),
        
        # 特殊组合
        ([1, 3, 2], "1,3,2组合"),
        ([3, 1, 2], "3,1,2组合"),
        ([2, 3, 1], "2,3,1组合"),
    ]
    
    for case, desc in edge_cases:
        expected = list(itertools.permutations(case))
        expected = [list(perm) for perm in expected]
        
        test_cases.append({
            "input": case,
            "expected": expected,
            "description": desc,
            "expected_count": len(expected)
        })
    
    # 随机生成测试用例
    random.seed(42)  # 设置随机种子确保可重复性
    
    for i in range(10):
        # 生成不同长度的数组
        length = random.randint(1, 5)  # 长度1-5，避免排列数过大
        
        # 生成不重复的随机数
        nums = random.sample(range(-10, 11), length)
        
        expected = list(itertools.permutations(nums))
        expected = [list(perm) for perm in expected]
        
        test_cases.append({
            "input": nums,
            "expected": expected,
            "description": f"随机用例{i+1}: 长度{length}",
            "expected_count": len(expected)
        })
    
    # 性能测试用例（较大数组）
    performance_cases = [
        ([1, 2, 3, 4, 5], "5个元素性能测试"),
        ([0, 1, 2, 3, 4, 5], "6个元素性能测试"),
        (list(range(7)), "7个元素性能测试"),
    ]
    
    for case, desc in performance_cases:
        expected = list(itertools.permutations(case))
        expected = [list(perm) for perm in expected]
        
        test_cases.append({
            "input": case,
            "expected": expected,
            "description": desc,
            "expected_count": len(expected),
            "is_performance_test": True
        })
    
    return test_cases

def calculate_factorial(n):
    """计算阶乘"""
    if n <= 1:
        return 1
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

def save_test_cases(filename="permutation_test_cases.json"):
    """保存测试用例到文件"""
    test_cases = generate_test_cases()
    
    # 添加统计信息
    total_permutations = sum(case["expected_count"] for case in test_cases)
    
    data = {
        "test_cases": test_cases,
        "statistics": {
            "total_test_cases": len(test_cases),
            "total_permutations": total_permutations,
            "max_array_length": max(len(case["input"]) for case in test_cases),
            "min_array_length": min(len(case["input"]) for case in test_cases if case["input"])
        }
    }
    
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    
    print(f"已生成 {len(test_cases)} 个测试用例")
    print(f"总排列数: {total_permutations}")
    print(f"保存到: {filename}")
    
    return data

def preview_test_cases():
    """预览测试用例"""
    test_cases = generate_test_cases()
    
    print("=== 测试用例预览 ===")
    print(f"总测试用例数: {len(test_cases)}")
    print("\n前10个测试用例:")
    print("-" * 60)
    
    for i, case in enumerate(test_cases[:10]):
        print(f"{i+1:2d}. {case['description']}")
        print(f"    输入: {case['input']}")
        print(f"    排列数: {case['expected_count']}")
        if case['expected_count'] <= 6:  # 只显示较小的排列
            print(f"    排列: {case['expected']}")
        print()

if __name__ == "__main__":
    # 预览测试用例
    preview_test_cases()
    
    # 生成并保存测试用例
    data = save_test_cases()
    
    # 显示统计信息
    stats = data["statistics"]
    print(f"\n=== 统计信息 ===")
    print(f"测试用例总数: {stats['total_test_cases']}")
    print(f"总排列数: {stats['total_permutations']}")
    print(f"最大数组长度: {stats['max_array_length']}")
    print(f"最小数组长度: {stats['min_array_length']}")
